package main

import (
	"fmt"
	"imooc.com/ccmouse/learngo/lang/retriever/value"
	"time"

	"imooc.com/ccmouse/learngo/lang/retriever/mock"
	"imooc.com/ccmouse/learngo/lang/retriever/real"
)

type Retriever interface {
	Get(url string) string
}

type Poster interface {
	Post(url string, form map[string]string) string
}

const url = "http://www.imooc.com"

func download(r Retriever) string {
	return r.Get(url)
}

func post(poster Poster) {
	poster.Post(url, map[string]string{
		"name":   "ccmouse",
		"course": "golang",
	})
}
// 接口的组合
type RetrieverPoster interface {
	Retriever
	Poster
}
// s对象: 既可以调用Poster的post方法，也可以调用Retriever的get方法
func session(s RetrieverPoster) string {
	s.Post(url, map[string]string{
		"contents": "another faked imooc.com",
	})
	return s.Get(url)
}

func main() {
	var r Retriever

	mockRetriever := mock.Retriever{Contents: "this is a fake imooc.com"}
	r = &mockRetriever
	inspect(r)

	r = &real.Retriever{
		UserAgent: "Mozilla/5.0",
		TimeOut:   time.Minute,
	}
	inspect(r)

	// 值类型没有强制校验，可以加&
	valueRetriever := value.Retriever{Contents: "this is a point value retriever"}
	r = &valueRetriever
	inspect(r)

	valuePointRetriever := value.Retriever{Contents: "this is a value retriever"}
	r = valuePointRetriever
	inspect(r)

	// Type assertion：通过r.(*结构体) 获取结构体类型
	if mockRetriever, ok := r.(*mock.Retriever); ok {

		fmt.Println(mockRetriever.Contents)
	} else if realRetriever,ok := r.(*real.Retriever); ok {
		fmt.Println("r is not a  realRetriever",realRetriever.TimeOut)

	}

	fmt.Println("Try a session with mockRetriever")
	fmt.Println(session(&mockRetriever))
}
// 根据type switch 不同类型，做不同输出
func inspect(r Retriever) {
	fmt.Println("Inspecting", r)
	fmt.Printf(" > Type:%T Value:%v\n", r, r)
	fmt.Print(" > Type switch: ")
	switch v := r.(type) { // r.(type) 得到Retriever肚子里的类型
	case *mock.Retriever:
		fmt.Println("[Contents:]", v.Contents)
	case *real.Retriever:
		fmt.Println("[UserAgent:]", v.UserAgent)
	case value.Retriever:
		fmt.Println("[value Contents]", v.Contents)
	case *value.Retriever:
		fmt.Println("[value point contents]", v.Contents)


	}
	fmt.Println()
}
